package com.zc.timedown.mapper;

import com.zc.timedown.domain.Achievement;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * ClassName:AchievementMapper
 * Description:
 * DateTime: 2025/10/22 14:01
 * Author: zc
 * Version: 1.0
 */
@Mapper
public interface AchievementMapper {

    /**
     * 插入成就记录
     */
    @Insert("INSERT INTO achievement (user_id, achievement_type, achieved_date, create_time) " +
            "VALUES (#{userId}, #{achievementType}, #{achievedDate}, NOW()) " +
            "ON DUPLICATE KEY UPDATE update_time = NOW()")
    int insertAchievement(Achievement achievement);

    /**
     * 查询用户的所有成就
     */
    @Select("SELECT * FROM achievement WHERE user_id = #{userId} ORDER BY achieved_date DESC")
    List<Achievement> selectAchievementsByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和成就类型查询成就
     */
    @Select("SELECT * FROM achievement WHERE user_id = #{userId} AND achievement_type = #{achievementType}")
    Achievement selectAchievementByUserAndType(@Param("userId") Long userId, @Param("achievementType") String achievementType);

    /**
     * 更新成就信息
     */
    @Update("UPDATE achievement SET achieved_date = #{achievedDate}, update_time = #{updateTime} WHERE id = #{id}")
    int updateAchievement(Achievement achievement);


}
